package com.tepth.appmanageservice.core;

import com.tepth.appmanageservice.tools.ext.ExtIO;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class Config {
    private HashMap<String, String> _map;

    private int _loginRetryInterval = -1;
    private int _heartRate = -1;
    private String _serverDomain = null;
    private int _serverPort = -1;

    public int getLoginRetryInterval() {
        if (this._loginRetryInterval == -1) {
            this._loginRetryInterval = Integer.valueOf(this._map.get("loginRetryInterval"));
        }
        return this._loginRetryInterval;
    }

    public int getHeartRate() {
        if (this._heartRate == -1) {
            this._heartRate = Integer.valueOf(this._map.get("heartRate"));
        }
        return this._heartRate;
    }

    public void setHeartRate(int heartbeatFrequency) {
        this._heartRate = heartbeatFrequency;
        this._map.put("heartRate", String.valueOf(heartbeatFrequency));

        this.saveConfig();
    }

    public String getServerDomain() {
        if (this._serverDomain == null) {
            this._serverDomain = (this._map.get("serverDomain"));
        }
        return this._serverDomain;
    }

    public void setServerDomain(String domain) {
        this._serverDomain = domain;
        this._map.put("serverDomain", this._serverDomain);

        this.saveConfig();
    }

    public int getServerPort() {
        if (this._serverPort == -1) {
            this._serverPort = Integer.valueOf(this._map.get("serverPort"));
        }
        return this._serverPort;
    }

    public void setServerPort(short port) {
        this._serverPort = port;
        this._map.put("serverPort", String.valueOf(this._serverPort));

        this.saveConfig();
    }

    public void init() {
        String text = ExtIO.getConfigFileText();
        if (text == null || text.trim().length() == 0) {
            this._map = new HashMap<>();
            this._map.put("loginRetryInterval", "5000");
            this._map.put("heartRate", "10000");

            this._map.put("serverDomain", "120.55.166.5");
            this._map.put("serverPort", "12398");
//			this._map.put("serverDomain", "192.168.1.232"); // 刘洋测试电脑
//			this._map.put("serverPort", "12398");

            this.saveConfig();
        } else {
            this.loadingConfig(text);
        }
    }

    private void loadingConfig(String path) {
        this._map = new HashMap<>();
        String[] rows = path.split("\r\n");
        for (String row1 : rows) {
            String row = row1.trim();
            if (row.length() > 0) {
                String[] cols = row.split("=");
                this._map.put(cols[0], cols[1]);
            }
        }
    }

    private void saveConfig() {
        Iterator<Entry<String, String>> iter = this._map.entrySet().iterator();
        StringBuilder sb = new StringBuilder();
        while (iter.hasNext()) {
            Entry<String, String> entry = iter.next();
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("\r\n");
        }
        ExtIO.saveConfigFile(sb.toString());
    }
}